package xxl.robotlegs.extensions.directcommandmap.impl 
{
	import xxl.robotlegs.extensions.commandcenter.api.ICommandExecutor;
	import xxl.robotlegs.extensions.commandcenter.api.ICommandMapping;
	import xxl.robotlegs.extensions.commandcenter.api.ICommandMappingList;
	import xxl.robotlegs.extensions.commandcenter.impl.CommandMapping;
	import xxl.robotlegs.extensions.commandcenter.impl.CommandPayload;
	import xxl.robotlegs.extensions.directcommandmap.api.IDirectCommandConfigurator;
	/**
	 * ...
	 * @author XXL
	 */
	public class DirectCommandMapper implements IDirectCommandConfigurator
	{
		private var _executor:ICommandExecutor;
		private var _mappings:ICommandMappingList;
		private var _mapping:ICommandMapping;
		
		public function DirectCommandMapper(executor:ICommandExecutor, mappings:ICommandMappingList, commandClass:Class) 
		{
			_executor = executor;
			_mappings = mappings;
			_mapping = new CommandMapping(commandClass);
			_mapping.setFireOnce(true);
			_mappings.addMapping(_mapping);
		}
		
		/* INTERFACE xxl.robotlegs.extensions.directcommandmap.api.IDirectCommandConfigurator */
		
		public function withExecuteMethod(name:String):IDirectCommandConfigurator 
		{
			_mapping.setExecuteMethod(name);
			return this;
		}
		
		public function withGuards(...guards):IDirectCommandConfigurator 
		{
			_mapping.addGuards.apply(null, guards);
			return this;
		}
		
		public function withHooks(...hooks):IDirectCommandConfigurator 
		{
			_mapping.addHooks.apply(null, hooks);
			return this;
		}
		
		public function withPayloadInjection(value:Boolean = true):IDirectCommandConfigurator 
		{
			_mapping.setPayloadInjectionEnabled(value);
			return this;
		}
		
		public function map(commandClass:Class):IDirectCommandConfigurator 
		{
			return new DirectCommandMapper(_executor, _mappings, commandClass);
		}
		
		public function execute(payload:CommandPayload = null):void 
		{
			_executor.executeCommands(_mappings.getList(), payload);
		}
		
	}

}